package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.UserIdentityAuthenticationDAO;
import com.ecoolex.ying.space.entity.UserIdentityAuthentication;
import com.ecoolex.ying.space.entity.UserIdentityAuthentication.Column;
import com.ecoolex.ying.space.entity.UserIdentityAuthenticationExample;
import com.ecoolex.ying.space.entity.UserIdentityAuthenticationExample.Criteria;
import com.ecoolex.ying.space.enums.UserIdentityAuthenticationDomainType;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserIdentityAuthenticationManager {
	
    @Autowired
    private UserIdentityAuthenticationDAO dao;
    
    public void verify(UserIdentityAuthentication record) {
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getUserId()),ResultCode.VERIFY.build(10), "用户id为空");
    	BizAssert.isTrue(Check.notNullAndGtZero(record.getDomainType()),ResultCode.VERIFY.build(11), "认证类型为空");
    	BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getLabel()),ResultCode.VERIFY.build(12), "认证标签为空");
    	BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getDescription()),ResultCode.VERIFY.build(13), "认证描述为空");
    }
    
    public UserIdentityAuthentication add(UserIdentityAuthentication record, String createUser) {
    	verify(record);
    	UserIdentityAuthentication old = dao.selectByPrimaryKey(record.getUserId());
    	if(Check.notNull(old)) {
    		old.setDomainType(record.getDomainType());
    		old.setLabel(record.getLabel());
    		old.setDescription(record.getDescription());
    		old.setDeleted(UserIdentityAuthentication.NOT_DELETED);
    		old.setCreateUser(createUser);
    		old.setCreateTime(Date.from(Instant.now()));
    		old.setUpdateUser(createUser);
    		old.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKey(old);
    		return old;
    	}else {
	    	record.setDeleted(UserIdentityAuthentication.NOT_DELETED);
	    	record.setCreateUser(createUser);
	    	record.setCreateTime(Date.from(Instant.now()));
	    	record.setUpdateUser(createUser);
	    	record.setUpdateTime(Date.from(Instant.now()));
	    	dao.insert(record);
	    	return record;
    	}
    }
    
    public UserIdentityAuthentication edit(UserIdentityAuthentication record,String updateUser) {
    	verify(record);
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
    	
    	return record;
    }
    
    public void delete(UserIdentityAuthentication record, String updateUser) {
    	record.setDeleted(UserIdentityAuthentication.IS_DELETED);
    	record.setUpdateUser(updateUser);
    	record.setUpdateTime(Date.from(Instant.now()));
    	
    	dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
    }
    
    public UserIdentityAuthentication getByUserId(Integer userId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(userId, UserIdentityAuthentication.NOT_DELETED);
    }
    
    public Pagination<UserIdentityAuthentication> query(Integer UserIdentityAuthenticationDomainType, Sorting[] sorts, Page page){
    	UserIdentityAuthenticationExample example = new UserIdentityAuthenticationExample();
    	Criteria criteria = example.createCriteria();
    	if(Check.notNullAndGtZero(UserIdentityAuthenticationDomainType)) {
    		criteria.andDomainTypeEqualTo(UserIdentityAuthenticationDomainType);
    	}
    	criteria.andDeletedEqualTo(UserIdentityAuthentication.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Sortings.builder(sorts).verify(Column :: valueOf).toSql());
    	return Pagination.create(page, dao.countByExample(example),() -> dao.selectByExample(example));
    }
    
    public List<UserIdentityAuthentication> queryByDomainType(UserIdentityAuthenticationDomainType type){
    	UserIdentityAuthenticationExample example = new UserIdentityAuthenticationExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDomainTypeEqualTo(type.real()).andDeletedEqualTo(UserIdentityAuthentication.NOT_DELETED);
    	return dao.selectByExample(example);
    }
}